/***
 * 自定义用户
 * */


//选项
function getOption(){
    $.ajax({
        type: apis.villageOption.method,
        url: apis.villageOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data.area;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '<option>';
            }
            $('#area_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//省市区
function area01ByPid(){
    $.ajax({
        type: apis.areaByPid.method,
        url: apis.areaByPid.url,
        data: { pid: 0 },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            h += '<option value="0">-选择-<option>';
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '<option>';
            }
            $('#area01').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//省市区
function area02ByPid(v){
    $.ajax({
        type: apis.areaByPid.method,
        url: apis.areaByPid.url,
        data: { pid: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            h += '<option value="0">-选择-<option>';
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '<option>';
            }
            $('#area02').html(h);
            //$('#areaid')[0].value = v;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//省市区
function area03ByPid(v){
    $.ajax({
        type: apis.areaByPid.method,
        url: apis.areaByPid.url,
        data: { pid: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            h += '<option value="0">-选择-<option>';
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '<option>';
            }
            $('#area03').html(h);
            $('#areaid')[0].value = v;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.villageIndex.method,
        url: apis.villageIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            getHtml(d);
            $('#pageTotal').html(res.total);
            var pageObj = formList.pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].area_name + '</td>';
        h += '<td>' + d[i].street + '</td>';
        h += '<td>' + d[i].map + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'village-edit.html\', ' + d[i].id + ');">';
        h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' <br>';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('tbody').html(h);
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//详情
function getOne(v){
    var formData = {
        aid: getAid(),
        id: v,
    };
    console.log(formData);
    $.ajax({
        type: apis.villageOne.method,
        url: apis.villageOne.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#title')[0].value = d.title;
            $('#area_str_name').html(d.area_str_name);
            $('#street')[0].value = d.street;
            $('#areaid')[0].value = d.area_id;
            if (d.map == '') {
                initMap({wd: '', jd: ''});
            } else {
                initMap({wd: d.lat, jd: d.lng});
                $('#map').html(d.map);
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改
function modify(){
    var img = $('#img').val();
    var formData = {
        aid: getAid(),
        id: id,
        title: $('#title').val(),
        areaid: $('#areaid').val(),
        street: $('#street').val(),
        map: $('#map').html(),
    };
    if (!formData.title) {
        msgPop({msg: '小区名称必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.villageModify.method,
        url: apis.villageModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function lay_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.villageDel.method,
            url: apis.villageDel.url,
            data: { aid: getAid(), id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//拾取坐标
var map, center, position, lat, lng;
function initMap(e) {
    position = document.getElementById('map');
    console.log(e);
    lat = e.wd == '' ? '39.984104' : e.wd;
    lng = e.jd == '' ? '116.307503' : e.jd;
    center = new TMap.LatLng(lat, lng);//设置中心点坐标
    //初始化地图
    map = new TMap.Map('container', {
        center: center,
    });
    //绑定点击事件
    map.on("click", function (evt) {
        lat = evt.latLng.getLat().toFixed(6);
        lng = evt.latLng.getLng().toFixed(6);
        position.innerHTML = lat + "," + lng;
    });
}


//地址转经纬度
function addressToMap(){
    var seakey = $('#seakey').val();
    position = document.getElementById('map');
    $.ajax({
        type: apis.villageAddressToMap.method,
        url: apis.villageAddressToMap.url,
        data: { address: seakey },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#container').html('');
            var lat = res.data.location.lat;
            var lng = res.data.location.lng;
            center = new TMap.LatLng(lat, lng);
            map = new TMap.Map('container', {
                center: center
            });
            position.innerHTML = lat + "," + lng;
            //绑定点击事件
            map.on("click", function (evt) {
                lat = evt.latLng.getLat().toFixed(6);
                lng = evt.latLng.getLng().toFixed(6);
                position.innerHTML = lat + "," + lng;
            });
        },
        error: function () {
            console.log('错误辣');
        }
    });
}